#include<bits/stdc++.h>
using namespace std;
int T;
int n;
typedef long long ll;
const int MAXN = 2e5 + 10;
int a[MAXN];
const int MAXM = 1e6 + 10;
int pos[MAXM];
struct seg
{
    //const int MAXN = 2e5 + 10;
    ll tr[MAXN << 2], tag[MAXN << 2];
    int pl, pr;
    ll d;
    void prep()
    {
        memset(tr, 0, sizeof tr);
        memset(tag, 0, sizeof tag);
    }
    void addtag(int p, ll d)
    {
        tr[p] += d;
        tag[p] += d;
    }
    void pushdown(int p)
    {
        addtag(p << 1, tag[p]);
        addtag(p << 1 | 1, tag[p]);
        tag[p] = 0;
    }
    void add(int l, int r, int p)
    {
        if (pl <= l && r <= pr) 
        {
            addtag(p, d);
            return;
        }
        pushdown(p);
        int mid = (l + r) >> 1;
        if (pl <= mid) add(l, mid, p << 1);
        if (pr > mid) add(mid + 1, r, p << 1 | 1);
        tr[p] = max(tr[p << 1], tr[p << 1 | 1]);
    }
    void modify(int l, int r, ll k)
    {
        pl = l + 1;
        pr = r + 1;
        d = k;
        add(1, n, 1);
    }
    ll ask(int l, int r, int p)
    {
        if (pl <= l && r <= pr) return tr[p];
        int mid = (l + r) >> 1;
        ll ret = 0;
        pushdown(p);
        if (pl <= mid) ret = max(ret, ask(l, mid, p << 1));
        if (pr > mid) ret = max(ret, ask(mid + 1, r, p << 1 | 1));
        return ret;
    }
    ll query(int l, int r)
    {
        pl = l + 1;
        pr = r + 1;
        return ask(1, n, 1);
    }
};
seg tr[2];
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> T;
    while (T--)
    {
        cin >> n;
        for(int i = 1; i <= n; ++i) cin >> a[i];
        tr[0].prep();
        tr[1].prep();
        memset(pos, 0, sizeof pos);
        for(int i = 1; i <= n; ++i)
        {
            for(int k = 0; k < 2; ++k)
            {
                ll ret = 0;
                if (i > 1) ret = tr[k ^ 1].query(0, i - 2);
                if (pos[a[i]]) ret = max(ret, tr[k ^ 1].query(pos[a[i]], pos[a[i]]) + a[i]);
                tr[k].modify(i - 1, i - 1, ret);
                //for(int j = 0; j < i - 1; ++j) cout << tr[k].query(j, j) << ' ';
                //cout << '\n';
            } 
            for(int k = 0; k < 2; ++k)
            {
                if (i > 1 && a[i - 1] == a[i]) tr[k].modify(0, i - 2, a[i]);
            }
            pos[a[i - 1]] = i - 1;
            //for(int j = 0; j <= i; ++j) cout << dp[i][j] << ' ';
            //cout << '\n';
        }
        //if (ans != dp[n][n]) cerr << "ciallo~" << endl;
        cout << tr[0].query(0, n - 1) << '\n';
    }
    return 0;
}